package com.example.demo.controller;

//import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
//import com.gbq.boot.web.bean.User;
//import com.gbq.boot.web.comment.qqLoginComment.AuthComment;
//import com.gbq.boot.web.service.UserService;
import com.example.demo.AuthComment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 登录Controller
 * @author 阿前
 * 2019年1月4日09:48:21
 */
@RestController
@RequestMapping("/login")
public class LoginController {

//    @Resource
//    private UserService userService;
//    @Resource
//    private AuthComment authComment;
//
//
//    @RequestMapping("/index")
//    public ModelAndView index(@ModelAttribute("user") User user) {
//        return new ModelAndView("/shop/index","user",user);
//    }
//
//
//    @RequestMapping("/login.html")
//    public ModelAndView toLogin() {
//        return new ModelAndView("login");
//    }
//
//    @RequestMapping("/qqLogin")
//    public void qqLogin(HttpServletResponse response)throws Exception{
//        //随机产生字符串
//        String state = StrUtil.uuid();
//        String url = authComment.getAuthUrl(state);
//        System.out.println(url);
//        //重定向
//        response.sendRedirect(url);
//    }
//
//
//    @GetMapping("/redirect")
//    public ModelAndView getData(@RequestParam(value = "code") String code, RedirectAttributes model){
//
//        //获取token
//        String accessToken = authComment.getAccessToken(code);
//        System.out.println("accessToken"+accessToken);
//
//        //获取openId
//        String openId = authComment.getOpenId(accessToken);
//        System.out.println("openId"+openId);
//
//        //获取用户信息
//        JSONObject userInfo = authComment.getUserInfo(accessToken, openId);
//        String myName = userInfo.getString("nickname");
//        User user = new User(null, "","111111",myName, System.currentTimeMillis(),"是",
//                userInfo.getString("figureurl_2"), userInfo.getString("gender")
//                ,1,1,"", "", openId);
//
//        //通过openId查询
//        User usr = userService.findUsrByOpenId(openId);
//        if (null != usr){
//            user.setId(usr.getId());
//            userService.updateById(user);
//        }else {
//            userService.insert(user);
//        }
//        model.addFlashAttribute("user", user);
//        //重定向
//        return new ModelAndView("redirect:/login/index");
//    }


}